package com.szp.springbootvue.service;

import com.szp.springbootvue.domain.Role;
import com.szp.springbootvue.mapper.UserMapper;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        List<GrantedAuthority> authorities = new ArrayList<>();
        //查找用户
        com.szp.springbootvue.domain.User user = userMapper.selectUserByName(s);
        List<Role> authority = userMapper.getAuthority(s);
        for (Role role : authority) {
            authorities.add(new SimpleGrantedAuthority(role.getName()));
        }
        return new User(s, user.getPassword(),authorities);
    }
}
